package com.example.hasee.zhcd.Activities.ShangWu;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.ChuanWu.LeavePortActivity;
import com.example.hasee.zhcd.Adapters.LeavePortAdapter;
import com.example.hasee.zhcd.Adapters.RFCompanyAdapter;
import com.example.hasee.zhcd.Base.BaseFragment;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;
import com.example.hasee.zhcd.View.NoScrollListView;
import com.example.hasee.zhcd.View.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.example.hasee.zhcd.R.id.lp_list;

/**
 * Created by hasee on 2018/6/1.
 */

public class RFCompanyFragment extends BaseFragment implements PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener {
    private static Context mContext;
    private EditText company;
    private Button btn_query;
    private PullToRefreshView mPullToRefreshView;
    private NoScrollListView company_list;
    private ScrollView scrollView;
    private RFCompanyAdapter adapter;
    private String strWhere = "";
    private String strFlag = "1";
    private int pagenum = 1;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private List<HashMap<String, String>> newlist = new ArrayList<HashMap<String, String>>();
    private Boolean isFrist = true;

    static RFCompanyFragment newInstance(Context context) {
        RFCompanyFragment newFragment = new RFCompanyFragment();
        mContext = context;
        return newFragment;
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZHSW_BYJList_Company":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        if (pagenum == 1) {
                            pagenum = pagenum + 1;
                            list.clear();
                            for (int i = 0; i < data.length(); i++) {
                                HashMap<String, String> lmap = new HashMap<String, String>();
                                lmap.put("fcgs", data.getJSONObject(i).getString("fcgs"));
                                lmap.put("fbyjje", data.getJSONObject(i).getString("fbyjje"));
                                lmap.put("fbyjye", data.getJSONObject(i).getString("fbyjye"));
                                lmap.put("fsumcost", data.getJSONObject(i).getString("fsumcost"));
                                lmap.put("frecmoney", data.getJSONObject(i).getString("frecmoney"));
                                list.add(lmap);
                            }
                            adapter = new RFCompanyAdapter(getActivity(), list);
                            company_list.setAdapter(adapter);
                        } else {
                            pagenum = pagenum + 1;
                            if (data.length() == 0) {
                                Toast.makeText(getActivity(), "无更多新消息！",
                                        Toast.LENGTH_LONG).show();
                                mPullToRefreshView.onFooterRefreshComplete();
                            } else {
                                newlist.clear();
                                for (int i = 0; i < data.length(); i++) {
                                    HashMap<String, String> lmap = new HashMap<String, String>();
                                    lmap.put("fcgs", data.getJSONObject(i).getString("fcgs"));
                                    lmap.put("fbyjje", data.getJSONObject(i).getString("fbyjje"));
                                    lmap.put("fbyjye", data.getJSONObject(i).getString("fbyjye"));
                                    lmap.put("fsumcost", data.getJSONObject(i).getString("fsumcost"));
                                    lmap.put("frecmoney", data.getJSONObject(i).getString("frecmoney"));
                                    newlist.add(lmap);
                                }
                                for (int j = 0; j < newlist.size(); j++) {
                                    list.add(newlist.get(j));
                                    adapter.notifyDataSetChanged();
                                }

                            }
                        }
                        mPullToRefreshView.onHeaderRefreshComplete();
                        mPullToRefreshView.onFooterRefreshComplete();
                    } else {
                        Toast.makeText(getActivity(), "无更多新消息！",
                                Toast.LENGTH_LONG).show();
                        mPullToRefreshView.onFooterRefreshComplete();
                    }
                    isFrist = false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getCompanyList() {
        strWhere = company.getText().toString().trim();
        if (isFrist) {
        } else {
            ProgressDialogUtils.createProgressDialog(getActivity(), "正在加载列表...");
        }

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strWhere", strWhere);
        map.put("strFlag", strFlag);
        map.put("strPageNum", pagenum + "");
        httpUtils.get("ZHSW_BYJList_Company", map, "ZHSW_BYJList");
    }

    @Override
    public int contentViewId() {
        return R.layout.fragment_company;
    }

    @Override
    public void set() {
        btn_query.setOnClickListener(this);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
        mPullToRefreshView.setLastUpdated(new Date().toLocaleString());
        getCompanyList();
    }

    @Override
    public void init(View view) {
        company = (EditText) view.findViewById(R.id.company);
        btn_query = (Button) view.findViewById(R.id.btn_query);
        scrollView = (ScrollView) view.findViewById(R.id.scrollView);
        mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.main_pull_refresh_view);
        company_list = (NoScrollListView) view.findViewById(R.id.company_list);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_query:
                pagenum = 1;
                getCompanyList();
                break;
        }
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        getCompanyList();
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        pagenum = 1;
        getCompanyList();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
}
